<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Luck;
use App\Models\LuckLogs;

class LuckController extends Controller{

    // 领取幸运值
    public function luck_value(Request $request){
        $openid = $request->openid;  //获取的用户openid
        $state = $request->state;  // 状态 1:签到 2:发红包 3:领红包
        $type = $request->type;  // 1:增加幸运值 2:减少幸运值
        $is_check = $request->is_check;  // 是否判断次数 true false

        $luck = new Luck();
        $data = $luck->updateLuckValue($openid,$state,$type,$is_check); // 更新幸运值
        if($data['code'] == 200){
            echo json_encode(['code'=>200,'massage'=>'成功','result'=>$data['result']]);
        }else{
            echo json_encode(['code'=>5000,'massage'=>'','result'=>'']);
        }

    }

    // 判断还有没有次数
    public function check_sign(Request $request){
        $openid = $request->openid;  //获取的用户openid
        $state = $request->state;  // 状态 1:签到 2:发红包 3:领红包
        $LuckLogs = new LuckLogs(); // 实例化对象
        $info = $LuckLogs->checkLuckLog($openid,$state);
        $Luck = new Luck();
        $data = $Luck->getLuck($openid);
        if($data){
            $data = $data[0];
        }
        if($info == true){
            echo json_encode(['code'=>200,'massage'=>'成功','result'=>$data]);
        }
        if($info == false){
            echo json_encode(['code'=>5000,'massage'=>'没有次数','result'=>$data]);
        }
    }
}
